const app = getApp()
Page({
  data: {
    tags: app.data.tags,
    record: {
      tagList: [],
      notes: '',
      type: '-',
      amount: 0,
      createdAt: new Date().toISOString()
    },
    recordList: app.data.recordList
  },
  onShow() {
    this.setData({
      tags: app.data.tags
    })
  },
  updateTag(e) {
    const {
      index
    } = e.detail
    const {
      tags
    } = this.data
    tags.forEach((v, i) => {
      i === index ? v.isActive = true : v.isActive = false;
    });
    this.setData({
      tags,
      'record.tagList': tags.filter(i => i.isActive)
    })

  },
  updateType(e) {
    const {
      type
    } = e.detail
    this.setData({
      'record.type': type
    })

  },
  updateNotes(e) {
    const {
      value
    } = e.detail
    this.setData({
      'record.notes': value
    })
  },
  saveRecord(e) {
    let {
      output
    } = e.detail
    const {
      record
    } = this.data
    const {
      recordList
    } = this.data
    this.setData({
      'record.amount': output
    })
    if (output === 0) {
      wx.showToast({
        title: '0就不用记了吧~',
        icon: 'error',
      })
      return;
    }
    if (record.tagList.length === 0 && output !== 0) {
      wx.showToast({
        title: '请选择标签哦~',
        icon: 'error',
      })
      return;
    } else {
      const record2 = JSON.parse(JSON.stringify(record))
      recordList.push(record2)
      wx.showToast({
        title: '记录成功',
        icon: 'success',
      })
      wx.setStorageSync('recordList', recordList)
      this.setData({
        recordList
      })
    }
  },
})